from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.sites.models import Site
from page.models import Page
from django.conf import settings

class SimpleConfig(models.Model):
    site = models.ForeignKey(Site, verbose_name=_('Site'))
    home_page = models.ForeignKey(Page, verbose_name=_('Home Page'))
    images_path = models.FilePathField(path=settings.MEDIA_ROOT, recursive=True, null=True, blank=True)

    def __unicode__(self):
        return self.site

    class Meta:
        ordering = ['site']
        verbose_name = _('Simple Config')
        verbose_name_plural = _('Simple Configs')


class Manager(models.Model):
    first_name = models.CharField(max_length=100, verbose_name=_('First Name'))
    second_name = models.CharField(max_length=100, verbose_name=_('Second Name'), blank=True, null=True)
    last_name = models.CharField(max_length=100, verbose_name=_('Last Name'))
    birthday = models.DateField(verbose_name=_('Birthday'), null=True, blank=True)
    icq = models.CharField(max_length=9, verbose_name=_('ICQ'), help_text=_('ICQ account number'))
    phone = models.CharField(max_length=20, verbose_name=_('Phone Number'))
    email = models.EmailField(verbose_name=_('Email'))
    active = models.BooleanField(default=True, verbose_name=_('Is active'))
    photo = models.CharField(max_length=255, verbose_name=_('Photo'), help_text=_('Path to the photo'))
    site = models.ManyToManyField(Site, verbose_name=_('Site'), related_name='sites', through='ManagerRelation')

    def __unicode__(self):
        if self.second_name:
            return ('%s %s %s') % (self.first_name, self.second_name, self.last_name)
        else:
            return ('%s %s') % (self.first_name, self.last_name)

    class Meta:
        ordering = ['first_name', 'second_name', 'last_name',]
        verbose_name = _('Manager')
        verbose_name_plural = _('Managers')

class ManagerRelation(models.Model):
    site = models.ForeignKey(Site, verbose_name=_('Site'))
    manager = models.ForeignKey(Manager)
    ordering = models.PositiveIntegerField(default=0, verbose_name=_('Ordering'))

    def __unicode__(self):
        return ('%s [%s]') % (self.manager, self.site.name)

    class Meta:
        ordering = ['site', 'manager']
        verbose_name = _('Managers to Sites Relation')
        verbose_name_plural = _('Managers to Sites Relations')

class Domain(models.Manager):
    site = models.ForeignKey(Site, verbose_name=_('Site'))
    domain = models.CharField(max_length=100, verbose_name=_('Domain'))

    def __unicode__(self):
        return ('%s [%s]') % (self.domain, self.site.name)